package com.rstrulak.managedBeans.controllers;

import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.PostConstruct;
import javax.servlet.ServletException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.rstrulak.dao.ProjectAttachmentsDao;
import com.rstrulak.dao.ProjectDao;
import com.rstrulak.dao.UserDao;
import com.rstrulak.dao.UserProjectDao;
import com.rstrulak.model.Project;
import com.rstrulak.model.ProjectAttachments;
import com.rstrulak.model.User;
import com.rstrulak.model.UserProject;
import com.rstrulak.utils.Constants;
import com.rstrulak.utils.JSFUtils;
import com.rstrulak.utils.NoteFile;

@Component("newProjectBean")
@Scope("request")
public class NewProjectBean implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = -945358760825549817L;

	@Autowired
	private ProjectDao projectDao;

	@Autowired
	private UserProjectDao userProjectDao;

	@Autowired
	private ProjectAttachmentsDao projectAttachmentsDao;

	@Autowired
	UserDao userDao;

	private Project project;

	private String projectName;

	private String projectDescription;

	private List<String> assignedUsers;

	private UserProject userProject;

	private Map<String, Long> allUsersMap;

	@PostConstruct
	public void init() {
		JSFUtils.reloadFlash();

		List<User> allUsersExceptUser = userDao.getAll();
		allUsersMap = new HashMap<String, Long>();

		for (User user : allUsersExceptUser) {
			allUsersMap.put(user.getFirstName() + " " + user.getLastName(), user.getId());
		}

	}

	public Project getProject() {
		return project;
	}

	public void setProject(Project project) {
		this.project = project;
	}

	public String getProjectName() {
		return projectName;
	}

	public void setProjectName(String projectName) {
		this.projectName = projectName;
	}

	public String getProjectDescription() {
		return projectDescription;
	}

	public void setProjectDescription(String projectDescription) {
		this.projectDescription = projectDescription;
	}

	public List<String> getAssignedUsers() {
		return assignedUsers;
	}

	public void setAssignedUsers(List<String> assignedUsers) {
		this.assignedUsers = assignedUsers;
	}

	public Map<String, Long> getAllUsersMap() {
		return allUsersMap;
	}

	public void setAllUsersMap(Map<String, Long> allUsersMap) {
		this.allUsersMap = allUsersMap;
	}

	public void saveProject(FileUploadBean fileUploadBean) throws ServletException, IOException {
		project = new Project();
		project.setDescription(projectDescription);
		project.setName(projectName);
		project.setOwner(JSFUtils.getLoggedUser());
		Long projectId = projectDao.save(project);

		userProject = new UserProject();
		userProject.setUser(JSFUtils.getLoggedUser());
		userProject.setProject(project);
		userProjectDao.save(userProject);

		for (String userId : assignedUsers) {
			userProject = new UserProject();
			userProject.setUser(userDao.getById(new Long(userId)));
			userProject.setProject(project);
			userProjectDao.save(userProject);
		}

		FileUploadBean uploadBean = fileUploadBean;
		ArrayList<NoteFile> filesArray = uploadBean.getFiles();
		List<ProjectAttachments> projectAttachmentsList = new ArrayList<ProjectAttachments>();

		for (int i = 0; i < filesArray.size(); i++) {
			ProjectAttachments projectAttachments = new ProjectAttachments();
			String oldName = filesArray.get(i).getName();
			StringBuilder finalName = new StringBuilder();
			finalName.append("projectId_");
			finalName.append(projectId);
			finalName.append("_fileName_");
			finalName.append(oldName);

			projectAttachments.setProject(project);
			projectAttachments.setFilePath(Constants.PROJECT_ATTACHMENTS_PATH + finalName);
			projectAttachments.setFileName(oldName);
			projectAttachmentsList.add(projectAttachments);

			filesArray.get(i).setName(finalName.toString());
		}

		fileUploadBean.saveProjectFiles();
		fileUploadBean.clearUploadData();
		if (projectAttachmentsList.size() > 0) {
			this.saveProjectAttachments(projectAttachmentsList);
		}

		JSFUtils.redirect("/protected/project.xhtml");
	}

	public void cancelAddingProject(FileUploadBean fileUploadBean) throws ServletException, IOException {
		fileUploadBean.clearUploadData();
		JSFUtils.redirect("/protected/project.xhtml");
	}

	public void saveProjectAttachments(List<ProjectAttachments> projectAttachmentsList) {
		for (ProjectAttachments projectAttachments : projectAttachmentsList) {
			projectAttachmentsDao.save(projectAttachments);
		}
	}

}
